<?php
/**
 * @title 46. 全排列 - 递归
 * @author start2004
 */

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer[][]
     */
    function permute($nums) {
        if(count($nums) == 1){
            return [$nums];
        } else {}

        /**
         * @since 2020-07-21 结果集
         */
        $result = [];

        /**
         * @since 2020-07-21 依次做头节点
         */
        foreach ($nums as $key=>$num){
            /**
             * @since 2020-07-21 其他字符全排列
             */
            $next = $nums;
            unset($next[$key]);
            $nextArray = self::permute($next);

            /**
             * @since 2020-07-21 组合
             */
            foreach ($nextArray as $nArray){
                array_unshift($nArray, $num);
                $result[] = $nArray;
            }
        }

        /**
         * @return
         */
        return $result;
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.21_4.php
 */
$datas = [
    [1,2,3,4,5],
    // [1,2,3,4,5,6],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->permute($data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
